package net.template.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.template.domain.template.*;
import net.template.repository.AttrDataRepository;
import net.template.repository.TemplateAttrRepository;
import net.template.repository.TemplateRepository;
import net.template.service.controller.BaseController;
import net.template.service.controller.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2018/9/16/016.
 */
@RestController
@CrossOrigin
@RequestMapping("preView")
public class ViewService extends BaseController {
    @Autowired
    private TemplateRepository templateRepository;

    @Autowired
    TemplateAttrRepository templateAttrRepository;

    @Autowired
    private AttrDataRepository attrDataRepository;
    /**
     * 预览模板
     * @param id
     * @return
     */
    @Transactional
    @GetMapping("/{id}")
    public Result<JSONObject> previewTemplate(@PathVariable("id") Integer id){
        Template template = templateRepository.load(id);
        List formItemList = new ArrayList<FormItem>();
        List tableItemList = new ArrayList<FormItem>();
        List tableItemDataList = new ArrayList<>();

        //List returnItemList = new ArrayList<>();

        for (String s : Arrays.asList(template.getSearchList().split(","))) {
            System.out.println(s);
            FormItem formItem = new FormItem();
            formItem.setType("input");
            formItem.setLabel(s);
            formItem.setKey(s);
            formItem.setSubtype("input");
            formItemList.add(formItem);
        }
        for (String s : Arrays.asList(template.getDealList().split(","))) {
            System.out.println(s);
            FormItem formItem = new FormItem();
            formItem.setType("button");

            if (("查询").equals(s)){
                formItem.setIcon("el-icon-search");
                formItem.setValue("search");
                formItem.setSubtype("primary");
            }else if (("新增").equals(s)){
                formItem.setIcon("el-icon-plus");
                formItem.setValue("add");
                //formItem.setClick("@click = \"attrPreView("  + id + ")\\");
                formItem.setSubtype("primary");
            }else if (("修改").equals(s)){
                formItem.setIcon("el-icon-edit");
                formItem.setValue("update");
                formItem.setSubtype("primary");
            }else if (("删除").equals(s)){
                formItem.setIcon("el-icon-delete");
                formItem.setValue("delete");
                formItem.setSubtype("primary");
            }
            formItem.setText(s);
            formItemList.add(formItem);
        }


        List<TemplateAttr> templateAttrs = templateAttrRepository.findByTemplateId(id);
        Map<String, String> map = new HashMap();
        for (TemplateAttr templateAttr : templateAttrs) {
            map.put(templateAttr.getAttrCn(), templateAttr.getAttr());
        }

        for (String s : Arrays.asList(template.getTableList().split(","))) {
            FormItem formItem = new FormItem();
            formItem.setText(s);
            formItem.setKey(map.get(s));
            formItem.setColumnName(s);
            tableItemList.add(formItem);
        }
        List<AttrData> attrDataList = attrDataRepository.findAll(new Specification<AttrData>() {
            @Override
            public Predicate toPredicate(Root<AttrData> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate predicate = criteriaBuilder.equal(root.get("templateId"), id);
                return predicate;
            }
        });
        Map<Integer, List<AttrData>> listMap = attrDataList.stream().collect(Collectors.groupingBy(t-> t.getDataId()));
        for (Integer attrData : listMap.keySet()) {
            List<AttrData> attrDatas = listMap.get(attrData);
            Map talbeItem = new HashMap();
            for (int i = 0; i < attrDatas.size(); i++) {
                talbeItem.put("id", attrDatas.get(i).getDataId());
                AttrData s = attrDatas.get(i);
                talbeItem.put(s.getAttrId(), s.getAttrValue());
            }
            tableItemDataList.add(talbeItem);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("templateId", id);
        jsonObject.put("formItemList", JSONArray.toJSON(formItemList));
        jsonObject.put("tableItemList", JSONArray.toJSON(tableItemList));
        jsonObject.put("tableItemDataList", JSONArray.toJSON(tableItemDataList));

        String jsonString = JSONObject.toJSONString(jsonObject);
        return Result.success(jsonObject);
    }
}
